package cn.donting.jme.swing.tab;

import cn.donting.jme.EditorContent;
import cn.donting.jme.core.state.SceneToolState;
import cn.donting.jme.editor.scene.Scene;
import cn.donting.jme.expansion.file.FileIcon;
import cn.donting.jme.expansion.tool.NullSceneTool;
import cn.donting.jme.expansion.tool.SceneTool;
import cn.donting.jme.swing.Tab;
import com.jme3.scene.Node;
import com.jme3.system.lwjgl.LwjglCanvas;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.Enumeration;
import java.util.List;

public class SceneViewTab extends Tab {

    public static final int TAB_INDEX = 4;

    private Scene scene;
    private File file;

    public SceneViewTab() {
        LwjglCanvas context = (LwjglCanvas) EditorContent.getJmeApplication().getContext();
        Canvas canvas = context.getCanvas();
        setLayout(new BorderLayout());
        JToolBar jToolBar = new JToolBar();
        List<SceneTool> sceneTools = EditorContent.getIocContainer().getBeansByTypes(SceneTool.class);
        SceneToolState sceneToolState = EditorContent.getJmeApplication().getStateManager().getState(SceneToolState.class);

        ButtonGroup buttonGroup = new ButtonGroup();
        for (SceneTool sceneTool : sceneTools) {
            SceneToolButton sceneToolButton = new SceneToolButton(sceneTool);
            sceneToolButton.setIcon(sceneTool.getIcon(16));
            buttonGroup.add(sceneToolButton);
            jToolBar.add(sceneToolButton);
            if (sceneTool.getClass().equals(NullSceneTool.class)) {
                sceneToolState.setSceneTool(sceneTool);
                sceneToolButton.setSelected(true);
            }else {
                sceneToolButton.setSelected(false);
            }
        }
//        add(canvas, BorderLayout.CENTER);
        add(jToolBar, BorderLayout.NORTH);
    }

    @Override
    public void setLoading() {

    }

    public void openScene(Scene scene, File file) {
        this.scene=scene;
        this.file=file;
        Node sceneNode = EditorContent.getEditorUI().getSceneNode();
        sceneNode.detachAllChildren();
        sceneNode.attachChild(scene.getRootNode());
        setTitle(file.getName());
        setIcon(FileIcon.getFileIcon(file, 16));
    }

    private class SceneToolButton extends JButton {
        private SceneTool sceneTool;

        public SceneToolButton(SceneTool sceneTool) {
            this.sceneTool = sceneTool;
            addActionListener(this::actionListener);
            setToolTipText(sceneTool.getName());
        }

        public void actionListener(ActionEvent e) {
            if (isSelected()) {
                return;
            }
            SceneToolState state = EditorContent.getJmeApplication().getStateManager().getState(SceneToolState.class);
            Enumeration<AbstractButton> elements = getModel().getGroup().getElements();
            while (elements.hasMoreElements()) {
                SceneToolButton sceneToolButton = (SceneToolButton) elements.nextElement();
                if (sceneToolButton.isSelected()) {
                    sceneToolButton.setSelected(false);
                }
            }
            setSelected(true);
            state.setSceneTool(sceneTool);
        }
    }
}
